import { onMounted, onBeforeUnmount } from 'vue'

export default function (proxy: any, refValue: string) {
  // type handleScrollType = (scrollTop: number) => void
  //处理但钱页面滚动事件
  function handleSetMainScroll(scrollTop: number) {
    proxy.$refs[refValue].scrollTop = scrollTop
  }
  //页面滚动时不断派发事件，并传参dom
  function handleMainScroll() {
    proxy.$mybus.emit('mainScroll', proxy.$refs[refValue])
  }

  onMounted(() => {
    proxy.$mybus.on('setMainScroll', handleSetMainScroll)
    proxy.$refs[refValue].addEventListener('scroll', handleMainScroll)
  })
  onBeforeUnmount(() => {
    proxy.$mybus.off('mainScroll')
    proxy.$mybus.off('setMainScroll', handleSetMainScroll)
    proxy.$refs[refValue].removeEventListener('scroll', handleMainScroll)
    //派发通知组件切换
    proxy.$mybus.emit('changeURL')
  })
}
